package com.cjh.crm.mapper;

import com.cjh.crm.bean.Customer;
import com.cjh.crm.model.ReportsResult;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CustomerMapper {


    //要在带注解的映射器接口类中使用动态 SQL，可以使用 script 元素。
    @Select({"<script>" +
            "select count(*) from customer" +
            "<if test='customer.cusNo != null'>where cus_no like concat('%',#{customer.cusNo},'%') </if>" +
            "<if test='customer.cusNo != null and customer.cusName != null'>and</if>" +
            "<if test='customer.cusNo == null and customer.cusName!=null'>where</if>" +
            "<if test='customer.cusName != null'>cus_name like concat('%',#{customer.cusName},'%') </if>" +
            "</script>"})
    int findCount(Map<String, Object> map);

    @Select({"<script>" +
            "select * from customer " +
            "<if test='customer.cusNo != null'>where cus_no like concat('%',#{customer.cusNo},'%') </if>" +
            "<if test='customer.cusNo != null and customer.cusName != null'>and </if>" +
            "<if test='customer.cusNo == null and customer.cusName!=null'>where</if>" +
            "<if test='customer.cusName != null'>cus_name like concat('%',#{customer.cusName},'%') </if>" +
            "limit #{start},#{limit}" +
            "</script>"})
    List<Customer> findCustomer(Map<String, Object> map);


    @Insert("INSERT INTO `customer`(`cus_name`, `cus_region`, `cus_url`, `cus_level`, `cus_credit`) " +
            "VALUES (#{cusName}, " +
            "#{cusRegion}, " +
            "#{cusUrl}, " +
            "#{cusLevel}, " +
            "#{cusCredit});")
    void save(Customer customer);


    @Delete("DELETE FROM customer WHERE cus_no = #{cusNo};")
    boolean delCustomer(Integer cusNo);

    @Update("UPDATE `customer` SET `cus_name` = #{cusName} , " +
            "`cus_region` = #{cusRegion} , " +
            "`cus_url` = #{cusUrl} , " +
            "`cus_level` = #{cusLevel} , " +
            "`cus_credit` = #{cusCredit} " +
            "WHERE `cus_no` = #{cusNo} ;")
    boolean costomerUpdate(Customer customer);

    @Select("select * from customer where cus_no =#{cusNo}")
    Customer findCustomerById(int cusNo);

    @Select("SELECT cus_region as region,COUNT(cus_no) as total FROM customer GROUP BY cus_region")
    List<ReportsResult> toCountCustomer();
}